package com.yin.active.mq;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.config.JmsListenerContainerFactory;

import javax.jms.Queue;
import javax.jms.Topic;

/**
 * @author: elvin
 */
@Configuration
public class ActiveMQConfig {
    //region 所有属性: 用户名地址密码, 队列名,主题名
    @Value("${queueName}")
    private String queueName;

    @Value("${topicName}")
    private String topicName;

    @Value("${spring.activemq.user}")
    private String usrName;

    @Value("${spring.activemq.password}")
    private  String password;

    @Value("${spring.activemq.broker-url}")
    private  String brokerUrl;
    //endregion


    /**
     * Desc: 创建队列   ||
     * Param: []    ||
     * Return: javax.jms.Queue    ||
     */
    @Bean
    public Queue queue(){



        return new ActiveMQQueue(queueName);
    }

    /**
     * Desc: 创建主题   ||
     * Param: []    ||
     * Return: javax.jms.Topic    ||
     */
    @Bean
    public Topic topic(){


        return  new ActiveMQTopic(topicName);
    }



    /**
     * Desc: 初始化连接工厂   ||
     * Param: []    ||
     * Return: org.apache.activemq.ActiveMQConnectionFactory    ||
     */
    @Bean
    public ActiveMQConnectionFactory connectionFactory() {

        
        return new ActiveMQConnectionFactory(usrName, password, brokerUrl);
    }




        /**
         * Desc: 设置队列   ||
         * Param: [connectionFactory]    ||
         * Return: org.springframework.jms.config.JmsListenerContainerFactory<?>    ||
         */
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerQueue(ActiveMQConnectionFactory connectionFactory){

        
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }

        /**
        * Desc: 设置主题   ||
        * Param: [connectionFactory]    ||
        * Return: org.springframework.jms.config.JmsListenerContainerFactory<?>    ||
        */
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic(ActiveMQConnectionFactory connectionFactory){


        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        //设置为发布订阅方式, 默认情况下使用的生产消费者方式
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(connectionFactory);
        return bean;
    }
}